﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Interactivity;
using System.Windows;
using System.Windows.Data;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;

namespace XWord.WPF.Behaviors
{
    public class ReanOnlyPropertyBehavior : Behavior<FrameworkElement>
    {
        //private Binding _binding;

        protected override void OnAttached()
        {
            //_binding = new Binding(Property) { Source = AssociatedObject };
            TreeView element = AssociatedObject as TreeView;
            if(element!=null)
                element.SelectedItemChanged +=new RoutedPropertyChangedEventHandler<object>(element_SelectedItemChanged);
          
            base.OnAttached();
        }

        void element_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var owner = (ListBox)sender;
            owner.SetValue(BindablePropertyItemProperty, owner.GetValue(ListBox.SelectedItemProperty));
        }

        void element_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            var owner = (TreeView)sender;
            BindablePropertyItem=owner.GetValue(TreeView.SelectedItemProperty);
        }

        public object BindablePropertyItem
        {
            get { return (object)GetValue(BindablePropertyItemProperty); }
            set { SetValue(BindablePropertyItemProperty, value); }
        }

        public static readonly DependencyProperty BindablePropertyItemProperty =
            DependencyProperty.Register("BindablePropertyItem", typeof(object), typeof(ReanOnlyPropertyBehavior), 
            new UIPropertyMetadata());

        
        public string Property { get; set; }

        public int MyProperty { get; set; }

    }

    public class ReadOnlyPropertyAction : TargetedTriggerAction<FrameworkElement>
    {
        public object BindablePropertyItem
        {
            get { return (object)GetValue(BindablePropertyItemProperty); }
            set { SetValue(BindablePropertyItemProperty, value); }
        }

        public static readonly DependencyProperty BindablePropertyItemProperty =
            DependencyProperty.Register("BindablePropertyItem", typeof(object), typeof(ReadOnlyPropertyAction),
            new UIPropertyMetadata(new PropertyChangedCallback((obj,args)=>{
                var element = obj as ReadOnlyPropertyAction;
                if (element.Reverse && args.NewValue != null)
                {
                    element.ReverseProperty();
                }
            })));

        private void ReverseProperty()
        {
            Reverse = false;
            AssociatedObject.GetType().GetProperty(Property).SetValue(AssociatedObject, BindablePropertyItem, null);
            
        }

        protected override void Invoke(object parameter)
        {
            if (Value != null)
                BindablePropertyItem = Value;
            else
            {
                BindablePropertyItem = AssociatedObject.GetType().GetProperty(Property).GetValue(AssociatedObject, null);
            }
        }

        public bool Reverse { get; set; }

        public string Value { get; set; }


        public string Property { get; set; }
    }
    public enum ElementType
    {
        TreeView,
        Selector,
        Other
    }
}
